//
//  VistorView.swift
//  Swiftsweibo
//
//  Created by 张鹏飞 on 16/1/1.
//  Copyright © 2016年 张鹏飞. All rights reserved.
//

import UIKit
import SnapKit

///// 访客视图的协议
//protocol VistorViewDelegate: NSObjectProtocol{
//
//    //代理方法  注册
//    func vistorViewDidRegister()
//    //代理方法  登录
//    func vistorViewDidLogin()
//}

class VistorView: UIView {
    
//    //代理属性
//    weak var delegate: VistorViewDelegate?
//    /// 监听方法
//    @objc private func clickLogin(){
//    
//        delegate?.vistorViewDidLogin()
//    }
//    @objc private func clickRegister(){
//    
//        delegate?.vistorViewDidRegister()
//    }
  
  
    //MARK: - 设置视图信息
    func setUpInfo(imageName: String?, title: String){
    
        messageLabel.text = title
        //如果图片是 nil 说明是首页直接返回
        guard let imgName = imageName else {
            // 播放动画
            startAnim()
            return
        }
        iconView.image = UIImage(named: imgName)
        //隐藏小房子
        homeIconView.hidden = true
        //将遮罩放到底部
        sendSubviewToBack(maskIconView)
    }
    
    /// 开起首页转轮动画
    private func startAnim(){
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //旋转弧度
        anim.toValue = 2 * M_PI
        //重复次数
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        //完成后是否移除
        anim.removedOnCompletion = false
        
        //添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
   

    //initWithFrame 是 UIView 的指定构造函数
    //使用纯代码创建控件时使用
    override init(frame: CGRect) {
         super.init(frame: frame)
        setUPUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setUPUI()
    }
    
    //MARK: - 懒加载控件
    /// 图标
    private lazy var iconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    /// 遮罩图像
    private lazy var maskIconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    /// 小房子
    private lazy var homeIconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    /// 文字 label
    private lazy var messageLabel:UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看")

    
    /// 注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
   
    /// 登录按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.orangeColor(), backImageName: "common_button_white_disable")

}

extension VistorView{

/// 设置 UI
    private func setUPUI(){
        //添加控件
        //添加顺序和 sb 添加顺序一致
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2. 设置自动布局
        /**
        - 添加约束需要添加到父视图上
        - 建议，子视图最好有一个统一的参照物！
        */
        // translatesAutoresizingMaskIntoConstraints 默认是 true，支持使用 setFrame 的方式设置控件位置
        // false 支持使用自动布局来设置控件位置
    
//        for v in subviews{
//        
//            v.translatesAutoresizingMaskIntoConstraints = false
//        }
        //1.图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        //小房子
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
//        //消息文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
//        //注册图标
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
//        //登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton.snp_top)
            make.right.equalTo(messageLabel.snp_right)
            make.size.equalTo(registerButton.snp_size)
        }
        //遮罩图像
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(registerButton.snp_bottom).offset(-36)
        }
    
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    
/// 添加监听方法
//        registerButton.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
//        
//        loginButton.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)

        
    }
}
